package com.hrsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsystem.entity.Employee;
import com.hrsystem.entity.OrgFirst;
import com.hrsystem.entity.OrgSecond;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.OrgSecondMapper;
import com.hrsystem.service.OrgSecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;



@RestController
public class OrgSecondController {

    @Resource
    OrgSecondMapper orgSecondMapper;

    @Autowired
    OrgSecondService orgSecondService;

    @Transactional
    @GetMapping("/selectSecondOrg/{id}")
    public Result selectSecondOrgList(@PathVariable(name = "id") Long id){
        QueryWrapper<OrgSecond> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fOrg_id",id);
        List<OrgSecond> orgSecondList = orgSecondMapper.selectList(queryWrapper);
        return Result.success(orgSecondList);
    }

    @Transactional
    @PostMapping("/AddSecondOrg")
    public Result addFirstOrg(@RequestBody OrgSecond orgSecond) {
        List<OrgSecond> orgSecondOrgList=orgSecondService.list();
        for(OrgSecond orgSecondOrg:orgSecondOrgList){
            if(Objects.equals(orgSecondOrg.getSorgName(), orgSecond.getSorgName())&&Objects.equals(orgSecondOrg.getForgId(), orgSecond.getForgId())){
                return Result.fail("该一级机构下的二级机构已存在");
            }
        }
        orgSecondService.saveOrUpdate(orgSecond);
        return Result.success(null);
    }
}
